<?php
/*
# $Id$
#
# Decorator.
# Provides a number of methods specific to HTML Views.
#
# Usage (from with your custom View class' constructor):
#	$this->html = new HtmlView($this);
#
# Usage (to add CSS/JS sources to your template):
#	$this->html->addStylesheets('/path/to/file.css', '/path/to/another.css');
#
# Usage (to render the CSS/JS <link>/<script> tags):
#	echo $this->html->getStylesheetsView()->renderAfter($this);
*/

/*
# @class HtmlView
*/
class HtmlView {

	/*
	# @property View $view
	# The View instance that is using this instance of the HtmlView.
	*/
	private $view;

	/*
	# @property array $javascripts
	# List of external Javascript sources (relative to document-root)
	*/
	public $javascripts = array();

	/*
	# @property array $stylesheets
	# List of external stylesheet sources (relative to document-root)
	*/
	public $stylesheets = array();

	/*
	# @property View $javascriptsView
	# The View that will be used to render the Javascript sources to $view.
	*/
	private $javascriptsView;

	/*
	# @property View $stylesheetsView
	# The View that will be used to render the stylesheet sources to $view.
	*/
	private $stylesheetsView;

	/*
	# @property string $urlSuffix
	# If you want all URLS generated by ::eUrl() to be suffixed with a string
	# (eg. ".html"), then set that suffix here.
	# It is up to your application to strip this suffix off the URL prior to it
	# being used elsewhere by Buan.
	*/
	public $urlSuffix = '';

	/*
	# @method HtmlView __construct()
	#
	# Constructor.
	*/
	public function __construct($view) {

		// Store reference to $view
		$this->view = $view;
	}

	/*
	# @method void setUrlSuffix( string $suffix )
	# $suffix	= Suffix
	#
	# Sets the URL suffix that will be appended to URLs generated by ::eUrl().
	*/
	public function setUrlSuffix($suffix) {
		$this->urlSuffix = $suffix;
	}

	/*
	# @method string|void e( string $str, [bool $echo] )
	# $str	= String to encode
	# $echo	= Whether or not to echo or return string
	#
	# Echos/returns the given string after converting applicable characters into
	# their HTML entitiy equivalents.
	*/
	public function e($str, $echo=TRUE) {
		$str = htmlentities($str, ENT_COMPAT, 'UTF-8');
		if($echo) {
			echo $str;
		}
		else {
			return $str;
		}
	}

	/*
	# @method eUrl( string $str, [bool $echo] )
	# $str	= String to urlize
	# $echo	= Whether or not to echo or return string
	#
	# Echos/returns the given string after converting applicable characters into
	# their HTML entitiy equivalents and adding the suffix defined in
	# $this->urlSuffix..
	*/
	public function eUrl($str, $echo=TRUE) {
		$url = htmlentities($str, ENT_COMPAT, 'UTF-8');
		if(strpos($url, "?")!==FALSE) {
			$url = str_replace("?", "{$this->urlSuffix}?", $url);
		}
		else {
			$url .= $this->urlSuffix;
		}

		if($echo) {
			echo $url;
		}
		else {
			return $url;
		}
	}

	/*
	# @method void addJavascripts( string $src0, string $src1, ..., string $srcN )
	# $src	= URL to external resource
	#
	# Adds external Javascript sources to the View.
	*/
	public function addJavascripts() {

		// Add Javascripts
		$view = $this->getJavascriptsView();
		$args = func_get_args();
		if(!empty($args)) {
			$this->javascripts = array_unique(array_merge($this->javascripts, $args));
		}

		// Update the Javascripts View's raw source
		$output = '';
		foreach($this->javascripts as $src) {
			$output .= "<script type=\"text/javascript\" src=\"{$src}\"></script>\n";
		}
		$this->javascriptsView->setRawSource($output);
	}

	/*
	# @method void addStylesheets( string $src0, string $src1, ..., string $srcN )
	# $src	= URL to external resource
	#
	# Adds external stylesheet sources to the View.
	*/
	public function addStylesheets() {

		// Add stylesheets
		$view = $this->getStylesheetsView();
		$args = func_get_args();
		if(!empty($args)) {
			$this->stylesheets = array_unique(array_merge($this->stylesheets, $args));
		}

		// Update the stylesheets View's raw source
		$output = '';
		foreach($this->stylesheets as $src) {
			$output .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$src}\" />\n";
		}
		$this->stylesheetsView->setRawSource($output);
	}

	/*
	# @method View getJavascriptsView()
	#
	# Returns the View that gets inserted into the parent View and adds the
	# external Javascript resources to the HTML document.
	*/
	public function getJavascriptsView() {
		if($this->javascriptsView===NULL) {
			$className = get_class($this->view);
			$this->javascriptsView = new $className;
		}
		return $this->javascriptsView;
	}


	/*
	# @method View getStylesheetsView()
	#
	# Returns the View that gets inserted into the parent View and adds the
	# external Stylesheet resources to the HTML document.
	*/
	public function getStylesheetsView() {
		if($this->stylesheetsView===NULL) {
			$className = get_class($this->view);
			$this->stylesheetsView = new $className;
		}
		return $this->stylesheetsView;
	}
}
?>